# DSEADemoApp build file. Replaces DSEADemoApp.pro (qmake) on all platforms.
#
# Binary output paths are kept exactly as qmake produced them, so the
# packaging and signing scripts need no changes:
#   Windows  Source/release/DSEADemoApp.exe
#   Linux    Source/DSEADemoApp
#   macOS    Source/DSEADemoApp.app

cmake_minimum_required(VERSION 3.21)

project(DSEADemoApp LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(Qt6 REQUIRED COMPONENTS
    Core
    Gui
    Widgets
    Network
    Xml
    Multimedia
    WebSockets
)

# The old .pro passed CONFIG+=qtquickcompiler. There is no QML in this project
# (checked: no .qml files, no QtQuick includes), so it did nothing and is dropped.

# qt_standard_project_setup() is not used on purpose: it needs Qt 6.3, and the
# Linux build image still has Qt 6.2.4. These three variables are what it would
# have set for us, and they work on every Qt 6 version.
# AUTORCC matters: without it Resource.qrc is never compiled and icons are blank.
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTORCC ON)

# The .ui files live in Forms/, not next to the sources.
set(CMAKE_AUTOUIC_SEARCH_PATHS ${CMAKE_CURRENT_SOURCE_DIR}/Forms)

set(DEMOAPP_SOURCES
    Source/DSEACCInterface.cpp
    Source/DemoAppWindow.cpp
    Source/LogManager.cpp
    Source/SdkClientRest.cpp
    Source/SdkInterfaceBase.cpp
    Source/WebSocketClient.cpp
    Source/SocketClient.cpp
    Source/About.cpp
    Source/main.cpp
)

# NOTE: Include/CustomToolButton.h was listed in DSEADemoApp.pro but the file
# does not exist in the repo. It caused a warning on every qmake build and is
# left out here.
set(DEMOAPP_HEADERS
    Include/CommonUtils.h
    Include/DSEACCInterface.h
    Include/DemoAppWindow.h
    Include/LogManager.h
    Include/LogSettings.h
    Include/ProtocolDialog.h
    Include/SdkClientRest.h
    Include/SdkInterfaceBase.h
    Include/WebSocketClient.h
    Include/SocketClient.h
    Include/About.h
)

set(DEMOAPP_FORMS
    Forms/DemoAppWindow.ui
    Forms/About.ui
)

qt_add_executable(DSEADemoApp
    WIN32
    MACOSX_BUNDLE
    ${DEMOAPP_SOURCES}
    ${DEMOAPP_HEADERS}
    ${DEMOAPP_FORMS}
    Resource.qrc
)

target_include_directories(DSEADemoApp PRIVATE
    ${CMAKE_CURRENT_SOURCE_DIR}
    ${CMAKE_CURRENT_SOURCE_DIR}/Include
    ${CMAKE_CURRENT_SOURCE_DIR}/../Dependencies/dseasdk/Dynamic/Include
)

target_compile_definitions(DSEADemoApp PRIVATE QT_DEPRECATED_WARNINGS)

target_link_libraries(DSEADemoApp PRIVATE
    Qt6::Core
    Qt6::Gui
    Qt6::Widgets
    Qt6::Network
    Qt6::Xml
    Qt6::Multimedia
    Qt6::WebSockets
)

# The version lives in version.h, which ChangeDemoAppVersion.py rewrites before
# the build. Read it here so the bundle carries the right version from the
# start, the same way DSEASDK sets its values before CMake runs.
file(READ ${CMAKE_CURRENT_SOURCE_DIR}/version.h DEMOAPP_VERSION_HEADER)
string(REGEX MATCH "VER_PRODUCTVERSION_STR[ \t]+\"([0-9.]+)\"" _match "${DEMOAPP_VERSION_HEADER}")
set(DEMOAPP_VERSION ${CMAKE_MATCH_1})
if(NOT DEMOAPP_VERSION)
    message(FATAL_ERROR "Could not read VER_PRODUCTVERSION_STR from version.h")
endif()
message(STATUS "DSEADemoApp version: ${DEMOAPP_VERSION}")

# Copyright comes from version.h, the same line the Windows .rc resource uses,
# so Windows and macOS show identical text. It follows the IPA-5224 format:
#   <app>, <company> A/S, Copyright (c) <year> <brand>. All Rights Reserved.
# Update it in version.h and both platforms follow.
string(REGEX MATCH "VER_LEGALCOPYRIGHT_STR[ \t]+\"([^\"]+)\"" _match "${DEMOAPP_VERSION_HEADER}")
set(DEMOAPP_COPYRIGHT ${CMAKE_MATCH_1})
if(NOT DEMOAPP_COPYRIGHT)
    message(FATAL_ERROR "Could not read VER_LEGALCOPYRIGHT_STR from version.h")
endif()
message(STATUS "DSEADemoApp copyright: ${DEMOAPP_COPYRIGHT}")

set(DSEASDK_LIB_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../Dependencies/dseasdk/Dynamic/Lib)

# ---------------------------------------------------------------- Windows ---
if(WIN32)
    # /MD and /MDd, matching the runtime overrides in the old .pro file.
    set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>DLL")

    target_sources(DSEADemoApp PRIVATE DSEA_DemoApp.rc)
    target_compile_options(DSEADemoApp PRIVATE /WX)

    # The .pro file had both an x86 and an x64 branch. Pick by pointer size.
    if(CMAKE_SIZEOF_VOID_P EQUAL 8)
        set(DSEASDK_WIN_ARCH x64)
    else()
        set(DSEASDK_WIN_ARCH x86)
    endif()
    message(STATUS "DSEA SDK arch: ${DSEASDK_WIN_ARCH}")

    target_link_libraries(DSEADemoApp PRIVATE
        hid
        Cfgmgr32
        user32
        $<$<CONFIG:Debug>:${DSEASDK_LIB_DIR}/${DSEASDK_WIN_ARCH}-Debug-Dcrt/DseaCallControlSdk.lib>
        $<$<NOT:$<CONFIG:Debug>>:${DSEASDK_LIB_DIR}/${DSEASDK_WIN_ARCH}-Release-Dcrt/DseaCallControlSdk.lib>
    )

    # QTDeploy.bat expects ..\Source\release\DSEADemoApp.exe
    set_target_properties(DSEADemoApp PROPERTIES
        RUNTIME_OUTPUT_DIRECTORY         ${CMAKE_CURRENT_SOURCE_DIR}/release
        RUNTIME_OUTPUT_DIRECTORY_RELEASE ${CMAKE_CURRENT_SOURCE_DIR}/release
        RUNTIME_OUTPUT_DIRECTORY_DEBUG   ${CMAKE_CURRENT_SOURCE_DIR}/release
    )
endif()

# ----------------------------------------------------------------- macOS ----
if(APPLE)
    target_include_directories(DSEADemoApp PRIVATE
        ${CMAKE_CURRENT_SOURCE_DIR}/../OpenSource/Openssl/include
    )

    if(CMAKE_OSX_ARCHITECTURES STREQUAL "arm64")
        set(DSEASDK_ARCH_DIR ${DSEASDK_LIB_DIR}/arm64-Release)
        set(OPENSSL_LIB_DIR  ${CMAKE_CURRENT_SOURCE_DIR}/../OpenSource/Openssl/lib_arm64)
    else()
        set(DSEASDK_ARCH_DIR ${DSEASDK_LIB_DIR}/x86_64-Release)
        set(OPENSSL_LIB_DIR  ${CMAKE_CURRENT_SOURCE_DIR}/../OpenSource/Openssl/lib)
    endif()
    message(STATUS "DSEA SDK libs: ${DSEASDK_ARCH_DIR}")

    target_link_libraries(DSEADemoApp PRIVATE
        ${DSEASDK_ARCH_DIR}/libDseaCallControlSdk.dylib
        ${OPENSSL_LIB_DIR}/libcrypto.3.dylib
        ${OPENSSL_LIB_DIR}/libssl.3.dylib
    )

    # Bundle icon, as ICON did in the .pro file.
    set(APP_ICON ${CMAKE_CURRENT_SOURCE_DIR}/images/DemoApp.icns)
    target_sources(DSEADemoApp PRIVATE ${APP_ICON})
    set_source_files_properties(${APP_ICON} PROPERTIES
        MACOSX_PACKAGE_LOCATION Resources
    )
    # Bundle identity is set here, before the build, so nothing has to rewrite
    # Info.plist afterwards. Same approach as DSEASDK, which sets its values in
    # CMakeLists.txt rather than editing the generated plist.
    set_target_properties(DSEADemoApp PROPERTIES
        MACOSX_BUNDLE_ICON_FILE            DemoApp.icns
        MACOSX_BUNDLE_BUNDLE_NAME          DSEADemoApp
        # Keep the identifier that every released build already carries, so the
        # installer recognises the app on disk and upgrades it in place.
        # Changing it would leave the old app behind on the first upgrade.
        MACOSX_BUNDLE_GUI_IDENTIFIER       com.yourcompany.DSEADemoApp
        MACOSX_BUNDLE_BUNDLE_VERSION       ${DEMOAPP_VERSION}
        MACOSX_BUNDLE_SHORT_VERSION_STRING ${DEMOAPP_VERSION}
        MACOSX_BUNDLE_COPYRIGHT            "${DEMOAPP_COPYRIGHT}"
        # copy_libraries_in_app_for_macosx() and macdeployqt expect the bundle
        # at Source/DSEADemoApp.app, so keep it out of the build folder.
        RUNTIME_OUTPUT_DIRECTORY  ${CMAKE_CURRENT_SOURCE_DIR}
    )
endif()

# ----------------------------------------------------------------- Linux ----
if(UNIX AND NOT APPLE)
    target_link_libraries(DSEADemoApp PRIVATE
        $<$<CONFIG:Debug>:${DSEASDK_LIB_DIR}/Debug/DseaCallControlSdk.so>
        $<$<NOT:$<CONFIG:Debug>>:${DSEASDK_LIB_DIR}/Release/DseaCallControlSdk.so>
        crypto
        ssl
    )

    # Same rpath the .pro file set through QMAKE_LFLAGS.
    set_target_properties(DSEADemoApp PROPERTIES
        INSTALL_RPATH          "/usr/lib/dsea-app/"
        BUILD_WITH_INSTALL_RPATH TRUE
        # CreateDemoAppInstaller.py reads the executable straight from Source/
        RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
    )
endif()
